package com.satan.vote.config;

import com.satan.vote.interceptor.SysInterceptor;
import com.satan.vote.properties.WhiteUrlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName WebMvcConfig
 * @Description TODO
 * @date 2022/2/19 20:25
 * @Version 1.0
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Value("${userImagesFilePath}")
    private String userImagesFilePath;
    @Value("${coverImagesFilePath}")
    private String coverImagesFilePath;
    @Value("${voteItemImagesFilePath}")
    private String voteItemImagesFilePath;


    @Autowired
    private WhiteUrlProperties whiteUrlProperties;

    private SysInterceptor  sysInterceptor;
    @Autowired
    public WebMvcConfig(SysInterceptor sysInterceptor) {
        this.sysInterceptor = sysInterceptor;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET","POST","PUT","OPTIONS","DELETE","PATCH")
                .allowedHeaders("*")
                .maxAge(3600);
    }

    //配置虚拟路径映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/userAvatar/**").addResourceLocations("classpath:"+userImagesFilePath);
        registry.addResourceHandler("/image/coverImgs/**").addResourceLocations("classpath:"+coverImagesFilePath);
                registry.addResourceHandler("/image/voteItemImgs/**").addResourceLocations("classpath:"+voteItemImagesFilePath);
    }
    //添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        System.out.println("whiteUrl = " + whiteUrlProperties);
        registry.addInterceptor(sysInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(whiteUrlProperties.getPatterns());
    }

}